package com.jd.security.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service("customUserService")
public class CustomUserService  implements UserDetailsService{

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
	    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println(authentication);
        System.out.println(authentication.getName());
    
        String client_id = authentication.getName();
        if ("client_2".equals(client_id)) {// 后台用户
            
        }else if ("client_1".equals(client_id)) { //C端用户
            
        }
	    
		List<SimpleGrantedAuthority> authorities = new ArrayList<>();
		// 用于添加用户的权限。只要把用户权限添加到authorities 就万事大吉。
		authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
		return new User(username,  new BCryptPasswordEncoder().encode("123"), authorities);
	}

}
